﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using Utils.Extensions;

namespace StockModel
{
    [DebuggerDisplay("Name = {Name}")]
    [Serializable]
    public class ExchangeMarket
    {
        #region Properties

        public ExchangeMarketName Name { get; private set; }
        public List<DateTime> Holidays { get; private set; }
        public TimeSpan PreOpen { get; private set; }
        public TimeSpan Open { get; private set; }
        public TimeSpan? Closing { get; private set; }
        public TimeSpan Closed { get; private set; }

        #endregion

        #region Public Methods

        public ExchangeMarket(ExchangeMarketName name, TimeSpan preOpen, TimeSpan open, TimeSpan? closing, TimeSpan closed)
        {
            this.Holidays = new List<DateTime>();

            this.Name = name;
            this.PreOpen = preOpen;
            this.Open = open;
            this.Closing = closing;
            this.Closed = closed;
        }

		public bool IsOpen(DateTime dateTime)
		{
			DateTime day = dateTime.Date;
			TimeSpan time = dateTime.TimeOfDay;

			return time >= this.PreOpen && time <= this.Closed && !day.IsWeekend() && !this.Holidays.Contains(day);
		}

		public QuoteType GetQuoteType(DateTime dateTime)
		{
			QuoteType result;

			TimeSpan time = dateTime.TimeOfDay;

			if (this.IsOpen(dateTime))
			{
				if (time < this.Open)
				{
					result = QuoteType.OpeningBid;
				}
				else if (this.Closing != null)
				{
					result = time < this.Closing ? QuoteType.Open : QuoteType.ClosingBid;
				}
				else
				{
					result = QuoteType.Open;
				}
			}
			else
			{
				result = QuoteType.Closed;
			}

			return result;
		}

        #endregion
    }
}
